﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.Adapters;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


namespace MyWebPagesStarterKit.Adapters
{
    /// <summary>
    /// Summary description for SiteMapPathAdapter
    /// </summary>
    public class SiteMapPathAdapter : WebControlAdapter
    {
        public SiteMapPathAdapter()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        protected override void RenderBeginTag(HtmlTextWriter writer)
        {
            SiteMapPath siteMapPath = Control as SiteMapPath;

            if (!String.IsNullOrEmpty(siteMapPath.Attributes["CssSelectorId"]))
            {
                writer.AddAttribute("id", siteMapPath.Attributes["CssSelectorId"]);

            }
            if (!String.IsNullOrEmpty(siteMapPath.CssClass))
            {
                writer.AddAttribute("class", siteMapPath.CssClass);
            }


            if (!String.IsNullOrEmpty(siteMapPath.Attributes["Container"]))
            {

                writer.RenderBeginTag(siteMapPath.Attributes["Container"]);
            }
            else
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
            }
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            try{
            SiteMapPath siteMapPath = Control as SiteMapPath;
            SiteMapProvider provider = siteMapPath.Provider;
            //Get current node and all ancestors
            SiteMapNode current = provider.GetCurrentNodeAndHintAncestorNodes(-1);
            //use a Stack to render nodes. 
            //since we start from the current node, and we need render from the parent downwards

            Stack<string> nodeStack = new Stack<string>();

            #region Render the nodes

            StringBuilder linkBuilder = new StringBuilder();
            XhtmlTextWriter xwri = new XhtmlTextWriter(new StringWriter(linkBuilder));
            #region Render the current node
            if (siteMapPath.RenderCurrentNodeAsLink)
            {

                xwri.AddAttribute("href", current.Url);
                if (String.IsNullOrEmpty(current.Description))
                {
                    xwri.AddAttribute("alt", current.Title);
                }
                else
                {
                    xwri.AddAttribute("alt", current.Description);
                }

                xwri.RenderBeginTag(HtmlTextWriterTag.A);
                if (!String.IsNullOrEmpty(current.Title))
                {
                    xwri.Write(current.Title);
                }
                else
                {
                    //xwri.Write("&nbsp;");
                }
                xwri.RenderEndTag();

            }
            else
            {
                if (!String.IsNullOrEmpty(current.Title))
                {
                    xwri.Write(current.Title);
                }
                else
                {
                    //xwri.Write("&nbsp;");
                }
            }




            xwri.Flush();
            xwri.Close();
            nodeStack.Push(linkBuilder.ToString());
            #endregion


            #region Render the current node's parents
            SiteMapNode parent = current.ParentNode;
            while (parent != null)
            {
                linkBuilder = new StringBuilder();
                xwri = new XhtmlTextWriter(new StringWriter(linkBuilder));


                xwri.AddAttribute("href", parent.Url);
                if (String.IsNullOrEmpty(parent.Description))
                {
                    xwri.AddAttribute("alt", parent.Title);
                }
                else
                {
                    xwri.AddAttribute("alt", parent.Description);
                }
                xwri.RenderBeginTag(HtmlTextWriterTag.A);
                if (!String.IsNullOrEmpty(parent.Title))
                {
                    xwri.Write(parent.Title);
                }
                else
                {
                    //xwri.Write("&nbsp;");
                }

                xwri.RenderEndTag();
                xwri.Flush();
                xwri.Close();
                nodeStack.Push(linkBuilder.ToString());
                parent = parent.ParentNode;

            }
            #endregion

            //ok now we have the stack.render the stack with the separator
            string pathItem;
            while (nodeStack.Count > 0)
            {
                pathItem = nodeStack.Pop();
                writer.Write(pathItem);


                //if there is a next item render the separator
                if (nodeStack.Count > 0)
                {
                    string sep = "/";
                    writer.WriteEncodedText(" " + sep + "");
                }
            }
            
            }catch{}  
        }

             

            #endregion
    }
}

